/*!
    \file  main.c 
    \brief this file realizes the MSC host
    
    \version 2018-03-26, V1.0.0, demo for GD32E10x
*/

#include "osObjects.h" 
#include "pincfg.h" 

void port_init(void);
void app_tick_init(void);

/*****************************************************************************//*!
*
* @brief   main routine.
*     
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
int main(void)
{
    SystemCoreClockUpdate();
    
    port_init();
    
    app_tick_init();
    
    osKernelInitialize(); // Initialize CMSIS-RTOS

    Init_Thread();
            
    if (osKernelGetState() == osKernelReady) 
    {
        osKernelStart(); // Start thread execution
    }
        
    osThreadYield();

     /*usb scan*/
    while (1) {
        __nop();
        osDelay(100000);
        __nop();
    }
}

/*****************************************************************************//*!
*
* @brief   ram clear.
*     
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/

void ram_clear(void)
{
    uint32_t *pdata;
    uint32_t cnt;
    
    cnt=0x20000/4;
    pdata = (uint32_t *)0x20000000;
    while(cnt--)
    {
        *pdata++=0;
    }
}

/*****************************************************************************//*!
*
* @brief   port init.
*     
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void port_init(void)
{
    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_GPIOB);
    rcu_periph_clock_enable(RCU_GPIOC);
    rcu_periph_clock_enable(RCU_GPIOD);
    rcu_periph_clock_enable(RCU_GPIOE);
    rcu_periph_clock_enable(RCU_GPIOF);
    rcu_periph_clock_enable(RCU_GPIOG);
    
    rcu_periph_reset_enable(RCU_GPIOARST);
    rcu_periph_reset_enable(RCU_GPIOBRST);
    rcu_periph_reset_enable(RCU_GPIOCRST);
    rcu_periph_reset_enable(RCU_GPIODRST);
    rcu_periph_reset_enable(RCU_GPIOERST);
    rcu_periph_reset_enable(RCU_GPIOFRST);
    rcu_periph_reset_enable(RCU_GPIOGRST);
    
    rcu_periph_reset_disable(RCU_GPIOARST);
    rcu_periph_reset_disable(RCU_GPIOBRST);
    rcu_periph_reset_disable(RCU_GPIOCRST);
    rcu_periph_reset_disable(RCU_GPIODRST);
    rcu_periph_reset_disable(RCU_GPIOERST);
    rcu_periph_reset_disable(RCU_GPIOFRST);
    rcu_periph_reset_disable(RCU_GPIOGRST);
    
    
    rcu_periph_clock_enable(RCU_CRC);
    
    //LED
    gpio_mode_set(GPIOC,GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, GPIO_PIN_1 | GPIO_PIN_2);
    gpio_output_options_set(GPIOC,GPIO_OTYPE_PP,GPIO_OSPEED_2MHZ,GPIO_PIN_1 | GPIO_PIN_2);
    
    gpio_mode_set(GPIOB,GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, GPIO_PIN_0 | GPIO_PIN_1  | GPIO_PIN_8 | GPIO_PIN_9);
    gpio_output_options_set(GPIOB,GPIO_OTYPE_PP,GPIO_OSPEED_2MHZ,GPIO_PIN_0 | GPIO_PIN_1  | GPIO_PIN_8 | GPIO_PIN_9);
    
    //LED
    gpio_mode_set(GPIOD,GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, GPIO_PIN_7);
    gpio_output_options_set(GPIOD,GPIO_OTYPE_PP,GPIO_OSPEED_2MHZ,GPIO_PIN_7);
    
    
    nvic_priority_group_set(NVIC_PRIGROUP_PRE3_SUB1);
    INTX_ENABLE();
}

/*****************************************************************************//*!
*
* @brief   app tick init.
*     
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
#define APP_TIM TIMER6
#define APP_TIM_IRQn                   TIMER6_IRQn

void app_tick_init(void)
{
    rcu_periph_clock_enable(RCU_TIMER5);
    rcu_periph_reset_enable(RCU_TIMER5RST);
    rcu_periph_reset_disable(RCU_TIMER5RST);
    
    TIMER_CTL0(APP_TIM) = TIMER_CTL0_UPS;
    TIMER_CTL1(APP_TIM) = 0;
    TIMER_DMAINTEN(APP_TIM) = TIMER_DMAINTEN_UPIE;
    TIMER_INTF(APP_TIM) = 0;
    TIMER_PSC(APP_TIM) = rcu_clock_freq_get(CK_APB1)/1000000-1;
    TIMER_CAR(APP_TIM) = 1999;
    
    NVIC_SetPriority(APP_TIM_IRQn, 0x0F);
    NVIC_ClearPendingIRQ(APP_TIM_IRQn);
    NVIC_EnableIRQ(APP_TIM_IRQn);
    
    TIMER_CTL0(APP_TIM) |= TIMER_CTL0_CEN;
}
